/*	BuildFactory class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildFactory.h"

void BuildFactory::Enter()
{
	DEBUG_PRINT("BuildFactory(%p) %s entering\n", this, GetName().c_str());

}

void BuildFactory::Execute()
{
	DEBUG_PRINT("BuildFactory(%p) %s executing\n", this, GetName().c_str());
	if(!builder)
	{
		std::list<BWAPI::Unit*>* scvs = (base)->GetUnitsByType(BWAPI::UnitTypes::Terran_SCV);
		std::list<BWAPI::Unit*>::iterator it = scvs->begin();
			
		if (!builder)
		{
			for (; it!=scvs->end(); it++)
			{
				if (((*it)->isGatheringMinerals() || (*it)->isIdle()) && !(*it)->isCarryingMinerals()) break;
			}
			if(it == scvs->end())
			{
				control->FinishWithSuccess();
				return;
			}
			builder = (*it);
		}
		
		BWAPI::TilePosition pos = BuildPlacer->getBuildLocationNear(base->GetBaseLocation(),BWAPI::UnitTypes::Terran_Factory);
		DEBUG_PRINT("BuildFactory(%p) attempting to build barracks at tile position(%d,%d).\n", this, pos.x(),pos.y());
		
		builder->build(pos,BWAPI::UnitTypes::Terran_Factory);
		BB->ResetOrder();
	}
	else if(builder)
	{
		if(!builder->exists())
		{
			builder = NULL;
			BB->Refund(BWAPI::UnitTypes::Terran_Factory);
		}
		else if (builder->isConstructing()&& builder->getBuildUnit())
			builder = NULL;
		else if (builder->getBuildType() == BWAPI::UnitTypes::Terran_Factory )//&& !builder->getBuildUnit())
		{

		}
		else
		{
			BB->Refund(BWAPI::UnitTypes::Terran_Factory);
			builder = NULL;
		}
	}
	control->FinishWithSuccess();
}

void BuildFactory::Exit()
{
	DEBUG_PRINT("BuildFactory(%p) %s exiting\n", this, GetName().c_str());
	
}

bool BuildFactory::CheckConditions()
{
	DEBUG_PRINT("BuildFactory(%p) %s checking conditions\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>* bar = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Barracks);
	std::list<BWAPI::Unit*>* fac = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Factory);
	std::list<BWAPI::Unit*>* marines = BB->GetUnitsByTypeGeneral(BWAPI::UnitTypes::Terran_Marine);
	if(marines)
	{
		if(marines->size() > 15)
		{
			if(fac)
			{
				if(fac->size() < 2*bases->size())
				{
					/*for(std::list<Base*>::iterator b = bases->begin(); b != bases->end(); b++)
					{
						if(fac = (*b)->GetBuildingsByType(BWAPI::UnitTypes::Terran_Factory))
						{
							if(fac->size() < 2)
							{
								base = *b;
								break;
							}
						}
					}*/
					base = bases->back();
					if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Factory) )
					{
						DEBUG_PRINT("BuildFactory(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
						BB->SpendResources(BWAPI::UnitTypes::Terran_Factory);
						return true;
					}
					else if(builder)
						return true;
				}
			}
			else
			{
				if(bar)
				{
					if(bar->size() > 2)
					{
						/*for(std::list<Base*>::iterator b = bases->begin(); b != bases->end(); b++)
						{
							if(*b)
							{	
								base = *b;
								break;
							}					
						}*/
						base = bases->back();
						if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Factory) )
						{
							DEBUG_PRINT("BuildFactory(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
							BB->SpendResources(BWAPI::UnitTypes::Terran_Factory);
							return true;
						}
						else if(builder)
							return true;
					}
				}
			}
		}
	}

	return false;
}